﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Collections;

namespace Projet_Synthese
{
    public class IdTexture
    {
        public const int Carre = 5;
        public const int Parallelogramme = 4;
        public const int TriangleGrand = 3;
        public const int TriangleMoyen = 2;
        public const int TrianglePetit = 1;

    }
    public class Tangram : IEnumerable
    {
        private bool rotation = false;
        private bool deplacement = false;

        public bool AssistanceRotation
        {
            get
            {
                return rotation;
            }
            set
            {
                rotation = value;
                update(0);
            }
        }
        public bool AssistanceDeplacement
        {
            get
            {
                return deplacement;
            }
            set
            {
                deplacement = value;
                update(1);
            }
        }

        private FormeGeometrique[] cases;

        public FormeGeometrique[] Cases
        {
            set 
            { 
                if(value.Length==7)
                    cases = value; 
            }
        }
        private Case[] formeOrignes;
        private int surface = 0;
        private Texture2D[] textures;

        public String Nom { get; set; }

        public Texture2D[] Textures
        {
            get { return textures; }
            set { textures = value; }
        }
        public Tangram()
        {
        }
        public Tangram(Texture2D [] texture)
        {
            this.textures = texture;
            formeOrignes = new Case[7];
            formeOrignes[0] = new Case(new Carre(texture, new Rectangle(77, 189, 59, 59)));
            formeOrignes[1] = new Case(new Parallelogramme(texture, new Rectangle(85, 247, 119, 42)));
            formeOrignes[2] = new Case(new TriangleGrand(texture, new Rectangle(181, 170, 111, 111)));
            formeOrignes[3] = new Case(new TriangleGrand(texture, new Rectangle(90, 344, 111, 111)));
            formeOrignes[4] = new Case(new TriangleMoyen(texture, new Rectangle(70, 128, 75, 75)));
            formeOrignes[5] = new Case(new TrianglePetit(texture, new Rectangle(192, 356, 58, 58)));
            formeOrignes[6] = new Case(new TrianglePetit(texture, new Rectangle(189, 283, 58, 58)));
            Initialisation();
        }

        public FormeGeometrique this[int i]
        {
            get
            {
                if (i < 0 || i > 7)
                    return null;
                return cases[i];
            }
            set
            {
                if (i < 0 || i > 7)
                    return;
                cases[i] = value;
            }
        }

        public Case[] FormeOrignes
        {
            get
            {
                return formeOrignes;
            }
            set
            {
                if (value.Length != 7)
                    return;
                formeOrignes = value;
            }
        }

        public bool EstComplete
        {
            get
            {
                return surface == 16;
            }

        }
        private void update(int k)
        {
            if (FormeOrignes != null)
            {
                for (int i = 0; i < 7; i++)
                {
                    if (k == 1)
                    {
                        FormeOrignes[i].FormeOrigne.IgnorePlacement = deplacement;
                        cases[i].IgnorePlacement = deplacement;
                    }
                    else
                    {

                        FormeOrignes[i].FormeOrigne.IgnoreRotation = rotation;
                        cases[i].IgnoreRotation = rotation;
                    }
                }
            }
            else
            {
                for (int i = 0; i < 7; i++)
                {
                    if (k == 1)
                        cases[i].IgnorePlacement = deplacement;
                    else
                        cases[i].IgnoreRotation = rotation;
                }
            }

        }
        public bool Placer(int i, int place, float distance = 2)
        {
            if (i < 0 || i > 7)
                return false;
            if (!formeOrignes[place].EstVerrouillé && formeOrignes[place].FormeOrigne == cases[i] &&
                (formeOrignes[place].FormeOrigne.IgnorePlacement || formeOrignes[place].FormeOrigne.RectangleDestination.Intersects(cases[i].RectangleDestination)))
            {
                
                Vector2 vect1 = new Vector2(formeOrignes[place].FormeOrigne.RectangleDestination.Center.X, formeOrignes[place].FormeOrigne.RectangleDestination.Center.Y);
                Vector2 vect2 = new Vector2(cases[i].RectangleDestination.Center.X, cases[i].RectangleDestination.Center.Y);
                if (formeOrignes[place].FormeOrigne.IgnorePlacement || (vect1 - vect2).Length() < distance)
                {
                    formeOrignes[place].FormePlacer = cases[i];
                    surface += cases[i].Surface;
                    cases[i] = null;
                    return formeOrignes[place].EstVerrouillé;
                }
            }
            return false;
        }
        public void Reinitialisation()
        {
            for(int i = 0; i < formeOrignes.Length; i++)
                formeOrignes[i].Clear();
            surface = 0;
            Initialisation();
        }
        public void Initialisation()
        {
            cases = new FormeGeometrique[7];
            cases[0] = new Carre(textures, new Rectangle(77, 189, 59, 59));
            cases[1] = new Parallelogramme(textures, new Rectangle(85, 247, 119, 42));
            cases[2] = new TriangleGrand(textures, new Rectangle(181, 170, 111, 111));
            cases[3] = new TriangleGrand(textures, new Rectangle(90, 344, 111, 111));
            cases[4] = new TriangleMoyen(textures, new Rectangle(70, 128, 75, 75));
            cases[5] = new TrianglePetit(textures, new Rectangle(192, 356, 58, 58));
            cases[6] = new TrianglePetit(textures, new Rectangle(189, 283, 58, 58));
        }
        public void DrawFormesOrigne(SpriteBatch sprite)
        {
            for (int i = 0; i < 7; i++)
                formeOrignes[i].Draw(sprite);
        }
        public void DrawFormes(SpriteBatch sprite)
        {
            for (int i = 0; i < 7; i++)
                if (cases[i] != null)
                    cases[i].Draw(sprite);
        }
        public void Draw(SpriteBatch sprite)
        {
            DrawFormesOrigne(sprite);
            DrawFormes(sprite);
        }

        public IEnumerator GetEnumerator()
        {
            return cases.GetEnumerator();
        }
    }
}
